package org.aeon.colorfulplanebattle.view.components;

import org.aeon.colorfulplanebattle.view.GameState;

import javax.swing.*;
import java.awt.*;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 13:06
 * @description:
 */
public class Scoreboard extends JLabel {
    private final GameState gameState;
    private int score;

    public Scoreboard(GameState gameState) {
        this.gameState = gameState;
        score = 0;
        this.setText(SCOREBOARD_PREFIX + score);
        this.setFont(SCOREBOARD_FONT);
        this.setBounds(WINDOW_WIDTH - 200, 10, 200, 30);
        this.gameState.put("score", score);
        this.gameState.put("maxScore", score);
        this.gameState.put("ScoreboardReset", (Runnable) this::reset);

    }

    private void reset() {
        gameState.put("maxScore", Math.max(gameState.pop("maxScore"), gameState.pop("score")));
        gameState.put("score", 0);
        gameState.put("isVictory", false);
        score = 0;
    }

    public int getScore() {
        if (((int) gameState.get("maxScore")) < score)
            gameState.put("isVictory", true);
        if (this.gameState.exist("destroy")) {
            int destroy = this.gameState.pop("destroy");
            score = score + destroy * 10;
            this.gameState.put("score", score);
        }
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    protected void paintComponent(Graphics g) {
        this.setText(SCOREBOARD_PREFIX + this.getScore());
        super.paintComponent(g);
    }
}
